package com.tan.api.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tan.common.dto.user.DictCreateDTO;
import com.tan.common.dto.user.DictTypeCreateDTO;
import com.tan.common.dto.user.DictTypeUpdateDTO;
import com.tan.common.dto.user.DictUpdateDTO;
import com.tan.common.query.user.DictQuery;
import com.tan.common.query.user.DictTypeQuery;
import com.tan.common.vo.user.DictMapVO;
import com.tan.common.vo.user.DictTypeVO;
import com.tan.common.vo.user.DictVO;
import com.tan.core.response.ApiResponse;
import com.tan.core.utils.StringUtils;
import com.tan.user.service.ISysDictService;
import com.tan.user.service.ISysDictTypeService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 字典controller
 * </p>
 *
 * @author Tan
 * @since 2025-01-27
 */
@RestController
@RequestMapping("/sys/dict")
public class SysDictController {

    @Resource
    private ISysDictTypeService dictTypeService;

    @Resource
    private ISysDictService dictService;


    @GetMapping("/pages")
    public ApiResponse<IPage<DictVO>> pages(DictQuery dictQuery) {
        return ApiResponse.success(dictService.DictPages(dictQuery));
    }

    @GetMapping("/get")
    public ApiResponse<List<DictVO>> dict(@RequestParam(value = "typeCode", required = false) String typeCode) {
        if (StringUtils.isBlank(typeCode)) {
            return ApiResponse.success(dictService.dictVOS());
        }
        return ApiResponse.success(dictService.getDict(typeCode));
    }

    @GetMapping("/list")
    public ApiResponse<List<DictMapVO>> dictMap() {
        return ApiResponse.success(dictService.dictMap());
    }

    @PostMapping("/create")
    public ApiResponse<Void> dictCreate(DictCreateDTO dictCreateDTO) {
        dictService.createDict(dictCreateDTO);
        return ApiResponse.success();
    }

    @PostMapping("/update")
    public ApiResponse<Void> dictUpdate(DictUpdateDTO dictUpdateDTO) {
        dictService.updateDict(dictUpdateDTO);
        return ApiResponse.success();
    }


    @PostMapping("/del/{dictId}")
    public ApiResponse<Void> del(@PathVariable("dictId") Long dictId) {
        dictService.delDict(dictId);
        return ApiResponse.success();
    }


    @GetMapping("/type-pages")
    public ApiResponse<IPage<DictTypeVO>> dictTypePages(DictTypeQuery dictTypeQuery) {
        return ApiResponse.success(dictTypeService.dictTypePages(dictTypeQuery));
    }


    @PostMapping("/type-create")
    public ApiResponse<Void> dictTypeCreate(@Valid @RequestBody DictTypeCreateDTO dictTypeCreateDTO) {
        dictTypeService.createDictType(dictTypeCreateDTO);
        return ApiResponse.success();
    }

    @PostMapping("/type-update")
    public ApiResponse<Void> dictTypeUpdate(DictTypeUpdateDTO dictTypeUpdateDTO) {
        dictTypeService.updateDictType(dictTypeUpdateDTO);
        return ApiResponse.success();
    }


    @PostMapping("/type-del/{dictTypeId}")
    public ApiResponse<Void> delDictType(@PathVariable("dictTypeId") Long dictTypeId) {
        dictTypeService.delDict(dictTypeId);
        return ApiResponse.success();
    }


}
